//
//  TimeRange.swift
//  hello
//
//  Created by 赵康 on 2024/12/24.
//

import Foundation

enum TimeRange {
	case day
	case week
	case month
	case year
	case custom(start: Date, end: Date)
	var days: Int {
		let calendar = Calendar.current
		let now = Date()
		
		switch self {
			case .day:
				return 1
			case .week:
				let weekStart = calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: now))!
				let weekEnd = calendar.date(byAdding: .day, value: 7, to: weekStart)!
				return calendar.dateComponents([.day], from: weekStart, to: weekEnd).day ?? 7
			case .month:
				let monthStart = calendar.date(from: calendar.dateComponents([.year, .month], from: now))!
				let nextMonthStart = calendar.date(byAdding: .month, value: 1, to: monthStart)!
				return calendar.dateComponents([.day], from: monthStart, to: nextMonthStart).day ?? 0
			case .year:
				let yearStart = calendar.date(from: calendar.dateComponents([.year], from: now))!
				let nextYearStart = calendar.date(byAdding: .year, value: 1, to: yearStart)!
				return calendar.dateComponents([.day], from: yearStart, to: nextYearStart).day ?? 0
			case let .custom(start, end):
				return calendar.dateComponents([.day], from: start, to: end).day ?? 0
		}
	}
	var redisKey: String {
		switch self {
			case .day: return "day"
			case .week: return "week"
			case .month: return "month"
			case .year: return "year"
			case .custom(let start, let end):
				let formatter = DateFormatter.yyyyMMdd
				return "custom_\(formatter.string(from: start))_\(formatter.string(from: end))"
		}
	}
	static func fromRedisKey(_ key: String) -> TimeRange? {
		if key == "day" { return .day }
		if key == "week" { return .week }
		if key == "month" { return .month }
		if key == "year" { return .year }
		
		if key.hasPrefix("custom_") {
			let components = key.split(separator: "_")
			guard components.count == 3,
						let startDate = DateFormatter.yyyyMMdd.date(from: String(components[1])),
						let endDate = DateFormatter.yyyyMMdd.date(from: String(components[2])) else {
				return nil
			}
			return .custom(start: startDate, end: endDate)
		}
		return nil
	}
  func getDateRange() -> (start: Date, end: Date) {
    let calendar = Calendar.mondayFirst
    let now = Date()
    
    switch self {
      case .day:
        let startOfDay = calendar.startOfDay(for: now)
        let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)!
        return (startOfDay, endOfDay)
        
      case .week:
          // 获取本周的开始和结束
        let components = calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: now)
        let startOfWeek = calendar.date(from: components)!
        // 使用日历API计算下一周的开始时间，而不是简单加7天
        let nextWeekComponents = DateComponents(weekOfYear: components.weekOfYear! + 1, yearForWeekOfYear: components.yearForWeekOfYear)
        let endOfWeek = calendar.date(from: nextWeekComponents) ?? calendar.date(byAdding: .day, value: 7, to: startOfWeek)!
        return (startOfWeek, endOfWeek)
        
      case .month:
          // 获取本月的开始和结束
        let components = calendar.dateComponents([.year, .month], from: now)
        let startOfMonth = calendar.date(from: components)!
        let endOfMonth = calendar.date(byAdding: .month, value: 1, to: startOfMonth)!
        return (startOfMonth, endOfMonth)
        
      case .year:
          // 获取本年的开始和结束
        let components = calendar.dateComponents([.year], from: now)
        let startOfYear = calendar.date(from: components)!
        let endOfYear = calendar.date(byAdding: .year, value: 1, to: startOfYear)!
        return (startOfYear, endOfYear)
        
      case .custom(let start, let end):
        let startDate = calendar.startOfDay(for: start)
        let endDateStart = calendar.startOfDay(for: end)
        let endDate = calendar.date(byAdding: .day, value: 1, to: endDateStart)!
        return (startDate, endDate)
    }
  }
}
extension TimeRange {
	func logTimeZoneInfo() {
		print("""
				时区信息:
				- 系统时区: \(TimeZone.current)
				- 应用时区: \(Calendar.current.timeZone)
				- 当前时间: \(Date())
				""")
	}
}
